//
//  ShenQingKaiDianSecViewController.m
//  1243
//
//  Created by ym on 16/5/31.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ShenQingKaiDianSecViewController.h"
#import <Photos/Photos.h>
#import "NetWorkTools.h"
@interface ShenQingKaiDianSecViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UITextFieldDelegate>
{
    NSString *judgeBtn;
}
@property (weak, nonatomic) IBOutlet UITextField *ShopName;
@property (weak, nonatomic) IBOutlet UITextField *ShopUserName;
@property (weak, nonatomic) IBOutlet UITextField *ShopUserIDCard;
@property (weak, nonatomic) IBOutlet UIButton *ZhengJianTu1;
@property (weak, nonatomic) IBOutlet UIButton *ZhengJianTu2;
@property (weak, nonatomic) IBOutlet UITextField *Address;
@property (weak, nonatomic) IBOutlet UITextField *Phone;
@property (weak, nonatomic) IBOutlet UITextField *Email;
@property (weak, nonatomic) IBOutlet UIButton *Finish;
@property (nonatomic,strong)NSMutableDictionary *ZhengJianDic;
@end

@implementation ShenQingKaiDianSecViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"申请开店";
    self.navigationController.navigationBar.translucent = NO;
   self.ZhengJianDic = [NSMutableDictionary dictionary];
    
    
    
    self.ShopName.delegate = self;
    self.ShopUserName.delegate = self;
     self.ShopUserIDCard.delegate = self;
     self.Address.delegate = self;
     self.Phone.delegate = self;
    self.Email.delegate = self;
    
}
- (IBAction)FinishBtn:(id)sender {
    NSDictionary *dict = [ReadLoginData readLoginData];
    NSString *userName = [dict objectForKey:@"UserName"];
    NSString *Password = [NSString stringWithFormat:@"%@",[dict objectForKey:@"Password"]];
    
     NSArray *ByteFileStringBusinessLicensePic = [[NSArray alloc] initWithObjects:_ZhengJianDic[@"执照图1"],_ZhengJianDic[@"执照图2"],nil];
    //
    if ( ByteFileStringBusinessLicensePic.count != 2 || self.ShopName.text.length==0 ||self.ShopUserName.text.length==0 || self.Phone.text.length == 0 ||self.ShopUserIDCard.text.length ==0|| self.Address.text.length==0    || self.Email.text.length == 0 ) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完整信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        
    }else{
        self.Finish.userInteractionEnabled = NO;
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.dic];
        [dic setObject:self.ShopName.text forKey:@"MerchantName"];
        [dic setObject:self.ShopUserName.text forKey:@"ContactName"];
        [dic setObject:userName forKey:@"LoginName"];
        [dic setObject:Password forKey:@"LoginPassword"];
        [dic setObject:self.Phone.text forKey:@"ContactPhone"];
        [dic setObject:self.ShopUserIDCard.text forKey:@"ContactMobile"];// 证件号
        [dic setObject:self.Address.text forKey:@"ContactAddress"];
        [dic setObject:self.Email.text forKey:@"ContactEmail"];
        [dic setObject:ByteFileStringBusinessLicensePic forKey:@"ByteFileStringContactCarPic"]; // 证件图
        

        [self netWorkGet:dic];
        
        
    }
    
    
    
    
  
    
    
}

- (void)netWorkGet:(NSMutableDictionary *)dic{
  
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dic AndMenthodName:@"MERCHANTINFOADD" completeBlock:^(id object) {
//        NSString *str = [object objectForKey:@"BackData"];
//        NSArray *arr = [NetWorkTools arrayWithJsonString:str];
//        NSLog(@"%@",object[@"Message"]);
     self.Finish.userInteractionEnabled = YES;
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"Message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alview show];
        

        
    }];
    
    
}


- (IBAction)ZhengJianTu1Btn:(id)sender {
    // 上传执照图1
    judgeBtn = @"执照图1";
    [self getImageFromIpc];
}

- (IBAction)ZhengJianTu2Btn:(id)sender {
    // 上传执照图2
    judgeBtn = @"执照图2";
    [self getImageFromIpc];

}

- (void)getImageFromIpc
{
    // 1.判断相册是否可以打开
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 4.设置代理
    ipc.delegate = self;
    // 5.modal出这个控制器
    [self presentViewController:ipc animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    // 销毁控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    // 转码
    NSData *data = UIImageJPEGRepresentation(info[UIImagePickerControllerOriginalImage], 1.0f);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    //    [_ZhengJianDic setValue:encodedImageStr forKey:judgeBtn];
    [_ZhengJianDic setObject:encodedImageStr forKey:judgeBtn];
    
   if ([judgeBtn isEqualToString:@"执照图1"]){
        [self.ZhengJianTu1 setBackgroundImage:info[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
    }else if ([judgeBtn isEqualToString:@"执照图2"]){
        [self.ZhengJianTu2 setBackgroundImage:info[UIImagePickerControllerOriginalImage] forState:UIControlStateNormal];
    }
    
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 11237891|| textField.tag ==11237892|| textField.tag == 11237893|| textField.tag == 11237894|| textField.tag == 11237895|| textField.tag == 11237896|| textField.tag == 11237897) {
        CGRect frame = textField.frame;
        int offset = frame.origin.y + 80 - (self.view.frame.size.height - 216.0);//键盘高度216
        
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        if(offset > 0)
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        
        [UIView commitAnimations];
    }
}
- (void)registerLogInTextField {

    
    [self.ShopName resignFirstResponder];
    [self.ShopUserName resignFirstResponder];
    [self.Phone resignFirstResponder];
    [self.ShopUserIDCard resignFirstResponder];
    [self.Address resignFirstResponder];
    [self.Email resignFirstResponder];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self registerLogInTextField];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);
    //    NSLog(@"%@", textField.text);
}


@end
